﻿using EFModel;
using IServices;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using xm.Controllers;

namespace Serives
{
    [Obsolete]
    public class TeRole : IRole
    {
        private readonly MyDBContext he;

        public TeRole(MyDBContext he)
        {
            this.he = he;
        }
        //角色删除
        public Task<int> Delt(Role role)
        {
            return Task.Run(() => {
                Role us = he.Role.Find(role.RoleID);
                he.Role.Remove(us);
                int num = he.SaveChanges();
                return num;
            });
        }
        //角色添加
        public Task<int> Insert(Role role)
        {
            return Task.Run(() => {
                he.Role.Add(role);
                int num = he.SaveChanges();
                return num;
            });
        }
        //当前角色查询
        public Task<Role> Que(int id)
        {
            return Task.Run(() => {
                he.Role.Find(id);
                
                return he.Role.Find(id);
            });
        }
        //角色查询
        public Task<ResultInfo> result(int currentPage, int pageSize)
        {
            return Task.Run(() => {
                var con = he.Role;
                var list = he.Role
                    .OrderBy(q => q.RoleID).Skip((currentPage - 1) * pageSize).Take(pageSize)
                    .ToList();
                int cont = con.Count();
                ResultInfo result = new ResultInfo()
                {
                    Data = list,
                    Message = "查询数据成功",
                    Status = 200,
                    Row = cont
                };
                return result;
            });
        }
        //角色权限查询
        public Task<ResultInfo> Quer(int pid)
        {
            return Task.Run(() => {
                var quan = he.RolPermissions.Where(e => e.RoleID == pid).Select(s => new { s.PermissionsID });
                ResultInfo result = new ResultInfo()
                {
                    Data = quan,
                    Message = "获取数据成功",
                    Status = 200
                };
                return result;
            });
        }
        //删除当前角色所有权限
        public Task<int> Delts(int pid)
        {
            return Task.Run(() => {
                string sql = $@"delete from [dbo].[RolPermissions]
                                where [RoleID]={pid}";

                int i = he.Database.ExecuteSqlCommand(sql);
                return i;
            });
        }
        //添加角色权限
        public Task<int> Inserts(int rid, int i)
        {
            return Task.Run(() => {
                string sql = $@"insert into [dbo].[RolPermissions]([RoleID], [PermissionsID])
                            values({rid},{i})";
                var zhi = he.Database.ExecuteSqlCommand(sql);
                return i;
            });
        }
        //修改角色权限
        public Task<int> Update(Role role)
        {
            return Task.Run(() => {
                Role role1 = he.Role.Find(role.RoleID);
                role1.Rolename = role.Rolename;
                role1.Texs = role.Texs;
                role1.Ysn = role.Ysn;
                int num = he.SaveChanges();
                return num;
            });
        }
    }
}
